package com.iseastar.dianxiaosan.home;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.home.adapter.TradeFinishAdapter;
import com.iseastar.dianxiaosan.model.TradeFinishResultBean;
import com.iseastar.dianxiaosan.model.TradeRecordBean;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import droid.frame.view.SwipeListViewOnScrollListener;
import droid.frame.view.xlist.SListView;

/**
 * 店小三 已交接的包裹
 * 
 * @author HuXinDan<br>
 *         2016-11-18上午10:23:25
 */
public class TradeFinishActivity extends BaseActivity2 {

	private SListView mListView;
	private TradeFinishAdapter mAdapter;
	private TradeRecordBean item;
	private TextView mTradFinishNum;

	protected void onCreate(Bundle savedInstanceState) {
		item = (TradeRecordBean) getIntent().getSerializableExtra("item");
		super.onCreate(savedInstanceState);
		AppHttp.getInstance().stationTradeRecordFinish(item.getPostStationId(), item.getCourierId(), item.getJoinTime());
	}

	protected void findViewById() {
		setContentView(R.layout.trade_finish);
		super.findViewById();
		getAppTitle().setCommonTitle("已交接的包裹");

		mListView = (SListView) findViewById(R.id.listview);
		mTradFinishNum = (TextView) findViewById(R.id.trad_finish_num);
		mListView.setFooterDividersEnabled(true);

		mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				AppHttp.getInstance().stationTradeRecordFinish(item.getPostStationId(), item.getCourierId(), item.getJoinTime());
			}
		});
		mListView.setOnScrollListener(new SwipeListViewOnScrollListener(mSwipeLayout));
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		// 交接完成
		case MsgID.trade_record_finish:
			onRefreshComplete();
			final ReqResult<TradeFinishResultBean> result = JSON.parser(msg.obj, TradeFinishResultBean.class);
			if (checkLoginStatus(result) && result.getResult() != null) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						TradeFinishResultBean items = result.getResult();
						mTradFinishNum.setText("共有" + items.getAlreadyParcelCount() + "个包裹");
						mAdapter = new TradeFinishAdapter(items.getAlreadyJoinList(), getContext(), mListView);
						mListView.setAdapter(mAdapter);
					}
				});
			} else {
				showToast(result.getMessage());
			}
			return true;
		default:
			return super.handleMessage(msg);
		}
	}
}
